<template>
    <div>
        <div style="height: 50px">
            <el-input
                    placeholder="请输入名称"
                    name="menuName"
                    v-model:value="menuName"
                    suffix-icon="el-icon-user"
                    style="width: 200px;padding-right: 5px;"
            >
            </el-input>
            <el-button class="el_margin_lr" type="primary" icon="el-icon-search" @click="search" >搜索</el-button>

            <el-button class="el_margin_lr" type="warning" icon="el-icon-search" @click="reset" >重置</el-button>
        </div>

        <div style="height: 50px">
            <el-button type="primary" @click="title='新增菜单',dialogFormVisible=true">新增<i class="el-icon-circle-plus-outline" style="margin-left: 2px"></i></el-button>
            <el-button type="danger" @click="batchDeleteUsers">批量删除<i class="el-icon-remove-outline" style="margin-left: 2px"></i></el-button>

        </div>

        <el-table row-key="id"
                  default-expand-all
                  :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                  :data="tableData"
                  header-cell-class-name="headerTableBg"
                  @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column prop="id" label="ID" width="140"></el-table-column>
            <el-table-column prop="name" label="菜单名" width="180"></el-table-column>
            <el-table-column prop="path" label="路径" width="180"></el-table-column>
            <el-table-column prop="pagePath" label="访问路径" width="180"></el-table-column>
            <el-table-column  label="图标" width="180" class-name="font18" label-class-name="font12">
                <template slot-scope="scope">
                    <i :class="scope.row.icon"></i>
                </template>
            </el-table-column>
            <el-table-column prop="description" label="菜单描述" ></el-table-column>

            <el-table-column prop="operation" label="操作" style="float: left">
                <template slot-scope="scope">
                    <el-button v-if="!scope.row.parentId" type="success" @click="toAddChildMenu(scope.row.id)" >增加子节点<i class="el-icon-edit" style="margin-left: 2px"></i></el-button>

                    <el-button type="success" @click="toEditMenu(scope.row)" >编辑<i class="el-icon-edit" style="margin-left: 2px"></i></el-button>

                    <el-popconfirm
                            class="el_margin_lr"
                            confirm-button-text='确定'
                            cancel-button-text='取消'
                            icon="el-icon-info"
                            icon-color="red"
                            title="确认删除该用户吗？"
                            @confirm="deleteMenu(scope.row.id)">
                        <el-button  type="danger" slot="reference">删除<i class="el-icon-remove-outline" style="margin-left: 2px"></i></el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>

        </el-table>

        <div >
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageIndex"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="totalNum">
            </el-pagination>
        </div>

        <el-dialog :title="title" style="text-align: center;" :visible.sync="dialogFormVisible" width="30%">
            <el-form label-width=80px >
                <el-form-item label="菜单名" >
                    <el-input v-model="menuForm.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="路径" >
                    <div v-if="!menuForm.id && !menuForm.parentId">
                        <el-input v-model="menuForm.path" autocomplete="off" placeholder="不输入,代表是母菜单"></el-input>
                    </div>
                    <div v-else-if="!menuForm.children|| menuForm.children.length==0">
                        <el-input v-model="menuForm.path" autocomplete="off" ></el-input>

                    </div>
                    <div v-else>
                        <el-input v-model="menuForm.path" autocomplete="off" :disabled="true" placeholder="该菜单为母菜单,不可修改路径"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="页面路径" >
                    <el-input v-model="menuForm.pagePath" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="图标" >
                        <el-select v-model="menuForm.icon" placeholder="请选择图标内容" style="width: 100%">
                            <el-option
                                    v-for="icon in icons"
                                    :key="icon.name"
                                    :label="icon.name"
                                    :value="icon.value">
                                <span style="float: left"><i :class="icon.value" style="margin-right: 15px;font-size: 15px;"></i>{{ icon.name }}</span>
                            </el-option>
                        </el-select>
                </el-form-item>
                <el-form-item label="菜单描述" >
                    <el-input v-model="menuForm.description" autocomplete="off"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addMenu">确 定</el-button>
            </div>
        </el-dialog>


    </div>

</template>

<script>
    import request from "@/utils/request";
    import store from "@/store";

    export default {
        name: "Menu",
        props:{
        },
        data(){
            return{
                tableData: [],
                form:{},
                pageIndex:1,
                totalNum:0,
                pageSize:10,
                multipleSelection:[],
                menuName:"",
                dialogFormVisible:false,
                menuForm:{},
                title:"",
                icons:[],
            }
        },
        created() {
            this.load();
            request.get("menu/queryAllMenusNum",{
                params:{
                    menuName: this.menuName
                }
            }).then(res=>{
                if (res.code === '200'){
                    this.totalNum=res.data;
                }
            });
        },
        methods:{
            toAddChildMenu(val){
                this.dialogFormVisible=true;
                this.menuForm.parentId=val;
            },
            load(){
                request.get("menu/queryAllMenus",{
                    params:{
                        pageIndex: this.pageIndex,
                        pageSize: this.pageSize,
                        menuName: this.menuName
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.tableData=res.data;
                        request.get("menu/queryAllMenusNum",{
                            params:{
                                menuName: this.menuName
                            }
                        }).then(res=>{
                            if (res.code === '200'){
                                this.totalNum=res.data;
                            }
                        })
                    }
                })
            },
            loadIcons(){
                request.get("meta/queryAllIcons").then(res=>{
                    if (res.code === '200'){
                        this.icons=res.data;
                    }
                });
            },
            toEditMenu(val){
                this.loadIcons();
                this.title='修改菜单';
                this.dialogFormVisible=true;
                this.menuForm=val;
            },
            addMenu(){
                request.post("menu/addMenu",this.menuForm).then(res=>{
                    if (res){
                        this.$message({
                            message: '修改用户成功',
                            type: 'success'
                        });
                        this.dialogFormVisible=false;
                        this.menuForm={};
                        this.updateMenuState();
                        this.load();
                    }else{
                        this.$message.error("修改用户失败")
                    }
                })
            },
            updateMenuState(){
                let user= JSON.parse(localStorage.getItem("user"));
                let menuIds= user.menus.map(val=>val.id);
                request.post("menu/updateMenuState",menuIds).then(res=>{
                    if (res.code==='200'){
                        user.menus=res.data;
                        localStorage.setItem("user",JSON.stringify(user));
                        location.reload();
                    }else{
                        this.$message.error("更新菜单状态失败")
                    }
                })
            },
            search(){
                this.load();
                request.get("menu/queryAllMenusNum",{
                    params:{
                        menuName: this.menuName
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.totalNum=res.data;
                    }
                })
            },
            changeState(id){
                request.get("menu/editMenuState",{
                    params:{
                        id : id,
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.load();
                    }
                })
            },
            download(url){
                window.open(url);
            },
            handleSelectionChange(val){
               this.multipleSelection=val;
            },
            batchDeleteUsers(){
                let ids=this.multipleSelection.map(val =>val.id);
                request.post("menu/batchDeleteMenu",ids).then(res=>{
                    if (res.code === '200'){
                        this.load();
                    }
                })
            },
            handleCurrentChange(val){
                this.pageIndex=val;
                this.load();
            },
            importMenus(){
                this.$message({
                    showClose: true,
                    message: '导入成功',
                    type: 'success'
                });
                this.load();
            },
            handleSizeChange(val){
                this.pageSize=val;
                this.load();
            },
            deleteMenu(id){
                request.get("menu/deleteMenu",{
                    params:{
                        id : id,
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.load();
                    }
                })

            },
            reset(){
                this.menuName="";
                this.load();
            }
        }
    }
</script>

<style scoped>

    /deep/ .font18{
        font-size: 18px;
    }

    /deep/ .font12{
        font-size: 12px;
    }
</style>